//
//  GetTradeListEngine.m
//  HuLaQuan
//
//  Created by liyan on 16/1/27.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "GetTradeListEngine.h"

#import "CurrentUser.h"

@implementation GetTradeListEngine

-(void)getTradeList
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getTradeList\" sysName=\"\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID];
    //
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_NOTIFY parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
            
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetTradeListFinish" object:result];
    }];
}

-(void)getTradeListWithIndex:(NSInteger)index
{
    NSString *randomKey = [self createKeyWithLength:24];
    
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"getTradeList\" sysName=\"\" sign=\"RSA\" serialNumber=\"%@\" termId=\"star_open_esa\" termAdd=\"23.23,23.45\" termBrand=\"iPhone\" osVersion=\"iPhone OS 9.1\" appVersion=\"1.0.0\" tokenValue=\"%@\" userId=\"%@\" channelId=\"88888888\"><fromPage>%ld</fromPage></msg>",[self getSerialNumber],[CurrentUser sharedInstance].token,[CurrentUser sharedInstance].ID,(long)index];
    //
    NSLog(@"%@ -- src --- ",src);
    
    NSString *final = [NSString stringWithFormat:@"%@|%@|%@",[self firstString],[self secondStringWithKey:randomKey],[self thirdStringWithBody:src andKey:randomKey]];
    
    NSLog(@"%@",final);
    [YuHTTPClient XMLDataWithUrl:SERVER_HOST_NOTIFY parameters:final success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            NSString *resString = [_DES decrypt:[item lastObject] withKey:randomKey];
            
            NSDictionary *resultDic = [self parseXMLFromYinLianConnection:resString];
            
            result = @{@"isSuccess":@YES,@"result":resultDic};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
            
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GetTradeListFinish" object:result];
    }];

}
@end
